#!/usr/bin/python3
import os, shutil, sys

"""
移动二级文件夹到一级，文件夹名重复将直接覆盖，请谨慎操作
"""
dirs = ['']

try:
    for dir in dirs:
        if not os.path.exists(dir):
            print(f'{dir} is not exists.')
            continue
        for f in os.listdir(dir):  # 遍历目标文件夹，移动二级文件夹到一级
            dir_path = os.path.join(dir, f)
            if os.path.isdir(dir_path):
                for path in os.listdir(dir_path):
                    filePath = os.path.join(dir_path, path)
                    if os.path.isdir(filePath):
                        rootPath = os.path.join(dir, path)
                        if os.path.exists(rootPath):
                            shutil.rmtree(rootPath)
                            print(f'Rewrite same name file: {rootPath}')
                        print(f'move {filePath} to {dir}')
                        shutil.move(filePath, dir)
        for f in os.listdir(dir):
            dir_path = os.path.join(dir, f)
            if os.path.isdir(dir_path) and not os.listdir(dir_path):
                os.rmdir(dir_path)
except Exception as e:
    print(f'Error: {e}')
print('Unpack "{}" finished'.format(dir))